package com.liuugoqing.structural.combination;

import java.util.ArrayList;
import java.util.List;

/**
 * 树枝节点（Composite）：定义树枝节点的行为，存储子节点，组合树枝节点和叶子节点形成一个树形结构
 *
 * @Author : liugq
 * @Date : 2022/7/5 16:03
 * @See : 参考类
 * @Since： JDK1.8
 * @Version : 1.0
 */
public class Menu extends MenuComponent {
    //节点下可以有其它节点
    private List<MenuComponent> menuComponents = new ArrayList<>();

    /**
     * 构造方法，指定节点内容
     *
     * @param name
     * @param level
     */
    public Menu(String name, int level) {
        this.name = name;
        this.level = level;
    }

    @Override
    public void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
        menuComponents.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int i) {
        return menuComponents.get(i);
    }

    @Override
    public void print() {
        for (int i = 0; i < level; i++)
            System.out.print("-");
        System.out.println(getName());

        for (MenuComponent menuComponent : menuComponents)
            menuComponent.print();

    }
}
